package com.wx.mp.dao.impl;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wx.mp.dao.TCarCityDao;
import com.wx.mp.domain.TCarCity;
import com.wx.mp.domain.TCarCityExample;
import com.wx.mp.mapper.TCarCityMapper;
import com.wx.mp.request.city.oper.RefreshCarCityReq;
import com.wx.mp.request.city.query.TCarCityReq;
import com.wx.mp.utils.constants.CarCityCacheEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: TCarCityDaoImpl.java
 * @Description:
 * @version: v1.0.0
 * @author: wangfs
 * @date: 2019-01-11 12:51
 * Modification History:
 * Date             Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-01-11     wangfs              v1.0.0               创建
 */
@Component
public class TCarCityDaoImpl implements TCarCityDao {

    private static final Logger logger = LoggerFactory.getLogger(TCarCityDaoImpl.class);

    @Autowired
    private TCarCityMapper carCityMapper;

    private static final Long TIMEOUT = 86400000L;//4 * 60* 60 * 1000=86400000毫秒(ms)

    //创建缓存
    private static final TimedCache<String, String> timedCache = CacheUtil.newTimedCache(TIMEOUT);

    /**
     * 查询出发地目的地信息.<br/>
     * @param carCityReq
     * @return
     */
    @Override
    public List<TCarCity> queryCarCityByType(TCarCityReq carCityReq) {
        logger.info("begin获取出发地、目的地信息...,入参={}", JSONObject.toJSON(carCityReq));
        List<TCarCity> carCitiesList = new ArrayList<>();
        //1.从缓存中加载
        String carCityJSON = timedCache.get(CarCityCacheEnum.FROM_TO_CACHE_KEY.getCode());
        logger.info("从缓存中获取获取出发地、目的地信息结果={}",JSONObject.toJSON(carCityJSON));
        if(StringUtils.isEmpty(carCityJSON)){//没有从缓存中获取到出发地目的信息
            logger.info("缓存中没有获取到，从数据库中加载...");
            TCarCityExample example = new TCarCityExample();
            example.createCriteria().andDataStateEqualTo(1);
            carCitiesList = carCityMapper.selectByExample(example);
            if(!CollectionUtils.isEmpty(carCitiesList)){
                //放入缓存
                timedCache.put(CarCityCacheEnum.FROM_TO_CACHE_KEY.getCode(),JSONObject.toJSONString(carCitiesList),TIMEOUT);
            }
        }else{
            carCitiesList = JSONArray.parseArray(carCityJSON,TCarCity.class);
        }
        logger.info("end获取出发地、目的地信息...,结果={}", JSONObject.toJSON(carCitiesList));
        return carCitiesList;
    }

    /**
     * 刷新出发地目的地缓存
     * @param refreshCarCityReq
     * @return
     */
    @Override
    public String refreshCarCity(RefreshCarCityReq refreshCarCityReq) {
        logger.info("begin刷新缓存出发地、目的地信息...,入参={}", JSONObject.toJSON(refreshCarCityReq));
        String flg="刷新缓存失败";
        List<TCarCity> carCitiesList = new ArrayList<>();
        TCarCityExample example = new TCarCityExample();
        example.createCriteria().andDataStateEqualTo(1);
        carCitiesList = carCityMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(carCitiesList)){
            //放入缓存
            timedCache.put(CarCityCacheEnum.FROM_TO_CACHE_KEY.getCode(),JSONObject.toJSONString(carCitiesList),TIMEOUT);
            flg = "刷新缓存成功";
        }
        logger.info("end刷新缓存出发地、目的地信息...,结果=【{}】",flg);
        return flg;
    }
}
